﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DungChung;
namespace CoVuaServer
{
    class VanDauDAO : DAO
    {
        public VanDauDAO(DataProvider dataProvider)
        {
            _dataProvider = dataProvider;
        }
        protected override object GetDataFromRow(DataTable dataTable, int index)
        {
            VanDauDTO vanDau = new VanDauDTO();
            vanDau.MaVanDau = Convert.ToInt32(dataTable.Rows[index]["MaVanDau"]);
            vanDau.NguoiChoi1 = dataTable.Rows[index]["NguoiChoi1"].ToString();
            vanDau.NguoiChoi2 = dataTable.Rows[index]["NguoiChoi2"].ToString();
            vanDau.ThoiGianTao = Convert.ToDateTime(dataTable.Rows[index]["ThoiGianTao"]);
            vanDau.KetQua = Convert.ToInt32(dataTable.Rows[index]["KetQua"]);
            return (object)vanDau;
        }
        public List<VanDauDTO> GetAllVanDau(string query)
        {
            List<VanDauDTO> vanDau = new List<VanDauDTO>();
            DataTable dataTable = _dataProvider.Execute(query);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                vanDau.Add((VanDauDTO)GetDataFromRow(dataTable, i));
            }
            return vanDau;
        }
        public void ThemVanDauMoi(VanDauDTO vanDau)
        {
            string query = "insert into VANDAU values(" + vanDau.MaVanDau + ",'" + vanDau.NguoiChoi1 + "','" +
                           vanDau.NguoiChoi2 + "','" + vanDau.ThoiGianTao.ToShortDateString() + "'," + vanDau.KetQua +  ")";
            _dataProvider.ExecuteNonQuery(query);
        }
    }
}
